package shoppingmall.manage.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import shoppingmall.manage.mapper.CommodityMapper;
import shoppingmall.manage.mapper.CommodityOrderMapper;
import shoppingmall.manage.mapper.CommodityStandardMapper;
import shoppingmall.manage.mapper.UserMapper;
import shoppingmall.manage.dto.PageBean;
import shoppingmall.manage.pojo.Commodity;
import shoppingmall.manage.pojo.CommodityOrder;
import shoppingmall.manage.pojo.CommodityStandard;
import shoppingmall.manage.pojo.User;
import shoppingmall.manage.service.CommodityOrderService;

import shoppingmall.manage.dto.CommodityOrderVO;

import java.text.SimpleDateFormat;
import java.util.*;


/**
 * 服务层接口实现类,管理平台订单相关方法
 */
@SuppressWarnings({"all"})
@Service
public class CommodityOrderServiceImpl implements CommodityOrderService{

    @Autowired
    private CommodityOrderMapper commodityOrderMapper;

    //商品mapper
    @Autowired
    private CommodityMapper commodityMapper;
    //平台会员用户mapper
    @Autowired
    private UserMapper userMapper;
    //商品规格mapper
    @Autowired
    private CommodityStandardMapper commodityStandardMapper;

    /**
     * 分页获取平台订单信息列表
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageBean getCommodityOrderList(int page, int pageSize) {
        //获取起始索引
        int start = (page - 1) * pageSize;
        //查询订单数据总条数
        int commodityOrderCount = commodityOrderMapper.getCommodityOrderCount();
        //分页查询的订单列表
        List<CommodityOrder> commodityOrderList = commodityOrderMapper.getCommodityOrderList(start, pageSize);

        //对查询到的集合数据中的相关字段进行替换commodity_id -> 商品名称  user_id -> 用户名  commodity_standard_id -> 规格名称
        List<Commodity> commodityList = commodityMapper.getAllCommodityList();  //查询所有商品数据
        List<User> allUserList = userMapper.getAllUserList();  //查询所有平台会员用户
        List<CommodityStandard> allCommodityStandardList = commodityStandardMapper.getAllCommodityStandardList();  //查询所有的规格信息
        List<CommodityOrderVO> commodityOrderVOList = new ArrayList<>();
        for(CommodityOrder o : commodityOrderList) {
            CommodityOrderVO commodityOrderVO = new CommodityOrderVO();
            commodityOrderVO.setId(o.getId());
            commodityOrderVO.setOrderId(o.getOrderId());
            for (int i = 0; i < commodityList.size(); i++) {
                if(commodityList.get(i).getCommodityId() == o.getCommodityId()) {
                    commodityOrderVO.setCommodityName(commodityList.get(i).getCommodityName());
                }
            }
            for (int j = 0; j < allUserList.size(); j++) {
                if(allUserList.get(j).getId() == o.getUserId()) {
                    commodityOrderVO.setUsername(allUserList.get(j).getUsername());
                }
            }
            for (int k = 0; k < allCommodityStandardList.size(); k++) {
                if(allCommodityStandardList.get(k).getId() == o.getCommodityStandardId()) {
                    commodityOrderVO.setCommodityStandardStr(allCommodityStandardList.get(k).getCommodityAttr());
                }
            }
            commodityOrderVO.setCommodityCount(o.getCommodityCount());
            commodityOrderVO.setOrderTotalPrice(o.getOrderTotalPrice());
            commodityOrderVO.setOrderState(o.getOrderState());
            commodityOrderVO.setLinkUser(o.getLinkUser());
            commodityOrderVO.setLinkPhone(o.getLinkPhone());
            commodityOrderVO.setLinkAddress(o.getLinkAddress());
            commodityOrderVO.setLogisticsInfo(o.getLogisticsInfo());
            commodityOrderVO.setOrderRemark(o.getOrderRemark());
            commodityOrderVO.setCreateTime(o.getCreateTime());
            commodityOrderVO.setUpdateTime(o.getUpdateTime());
            //加入arrayList中
            commodityOrderVOList.add(commodityOrderVO);
        }

        //返回结果
        return new PageBean(commodityOrderCount,commodityOrderVOList);
    }

    /**
     * 修改某个平台订单信息
     * @param commodityOrder
     * @return
     */
    @Override
    public boolean updateCommodityOrder(CommodityOrder commodityOrder) {
        try {
            commodityOrderMapper.updateCommodityOrder(commodityOrder);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 删除平台某个订单信息
     * @param
     * @return
     */
    @Override
    public boolean deleteCommodityOrder(String orderId) {
        try {
            commodityOrderMapper.deleteCommodityOrder(orderId);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /*********************************复杂业务*************************************************/

    /**
     * 批量修改订单信息(通过字符串订单id  将已支付的订单状态---->已发货的订单状态)
     */
    @Override
    public boolean updateOrderStateByOrderIds(List<String> orderIds, String orderState) {
        try {
            commodityOrderMapper.updateOrderStateByIds(orderIds, orderState);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 批量删除订单(通过字符串订单id 数组集合，批量删除订单)
     */
    @Override
    public boolean deleteOrdersByOrderIds(List<String> orderIds) {
        try {
            commodityOrderMapper.deleteOrders(orderIds);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 按照选择的开始日期到结束日期筛选订单 startDate:"" endDate:""
     */
    @Override
    public PageBean getCommodityOrderListByDate(Date startDate, Date endDate) {
        //调用mapper层方法
        List<CommodityOrder> commodityOrderListByDate = null;
        try {
            //对日期格式化处理
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String formattedEndDate = sdf.format(endDate);
            String formattedStartDate = sdf.format(startDate);
            commodityOrderListByDate = commodityOrderMapper.getCommodityOrderListByDate(formattedStartDate, formattedEndDate);
        } catch (Exception e) {
            e.printStackTrace();
        }

        //对查询到的集合数据中的相关字段进行替换commodity_id -> 商品名称  user_id -> 用户名  commodity_standard_id -> 规格名称
        List<Commodity> commodityList = commodityMapper.getAllCommodityList();  //查询所有商品数据
        List<User> allUserList = userMapper.getAllUserList();  //查询所有平台会员用户
        List<CommodityStandard> allCommodityStandardList = commodityStandardMapper.getAllCommodityStandardList();  //查询所有的规格信息
        List<CommodityOrderVO> commodityOrderVOList = new ArrayList<>();
        for(CommodityOrder o : commodityOrderListByDate) {
            CommodityOrderVO commodityOrderVO = new CommodityOrderVO();
            commodityOrderVO.setId(o.getId());
            commodityOrderVO.setOrderId(o.getOrderId());
            for (int i = 0; i < commodityList.size(); i++) {
                if(commodityList.get(i).getCommodityId() == o.getCommodityId()) {
                    commodityOrderVO.setCommodityName(commodityList.get(i).getCommodityName());
                }
            }
            for (int j = 0; j < allUserList.size(); j++) {
                if(allUserList.get(j).getId() == o.getUserId()) {
                    commodityOrderVO.setUsername(allUserList.get(j).getUsername());
                }
            }
            for (int k = 0; k < allCommodityStandardList.size(); k++) {
                if(allCommodityStandardList.get(k).getId() == o.getCommodityStandardId()) {
                    commodityOrderVO.setCommodityStandardStr(allCommodityStandardList.get(k).getCommodityAttr());
                }
            }
            commodityOrderVO.setCommodityCount(o.getCommodityCount());
            commodityOrderVO.setOrderTotalPrice(o.getOrderTotalPrice());
            commodityOrderVO.setOrderState(o.getOrderState());
            commodityOrderVO.setLinkUser(o.getLinkUser());
            commodityOrderVO.setLinkPhone(o.getLinkPhone());
            commodityOrderVO.setLinkAddress(o.getLinkAddress());
            commodityOrderVO.setLogisticsInfo(o.getLogisticsInfo());
            commodityOrderVO.setOrderRemark(o.getOrderRemark());
            commodityOrderVO.setCreateTime(o.getCreateTime());
            commodityOrderVO.setUpdateTime(o.getUpdateTime());
            //加入arrayList中
            commodityOrderVOList.add(commodityOrderVO);
        }

        return new PageBean(commodityOrderVOList.size(),commodityOrderVOList);
    }

    /**
     * 按照订单状态筛选订单(未支付，已支付，待发货，已发货，运输中，已收货，已评价，已取消，已退货)
     */
    @Override
    public PageBean getCommodityOrderListByState(String orderState) {
        List<CommodityOrder> commodityOrderList = null;
        try {
            commodityOrderList = commodityOrderMapper.getCommodityOrderListByState(orderState);
        } catch (Exception e) {
            e.printStackTrace();
        }

        //对查询到的集合数据中的相关字段进行替换commodity_id -> 商品名称  user_id -> 用户名  commodity_standard_id -> 规格名称
        List<Commodity> commodityList = commodityMapper.getAllCommodityList();  //查询所有商品数据
        List<User> allUserList = userMapper.getAllUserList();  //查询所有平台会员用户
        List<CommodityStandard> allCommodityStandardList = commodityStandardMapper.getAllCommodityStandardList();  //查询所有的规格信息
        List<CommodityOrderVO> commodityOrderVOList = new ArrayList<>();
        for(CommodityOrder o : commodityOrderList) {
            CommodityOrderVO commodityOrderVO = new CommodityOrderVO();
            commodityOrderVO.setId(o.getId());
            commodityOrderVO.setOrderId(o.getOrderId());
            for (int i = 0; i < commodityList.size(); i++) {
                if(commodityList.get(i).getCommodityId() == o.getCommodityId()) {
                    commodityOrderVO.setCommodityName(commodityList.get(i).getCommodityName());
                }
            }
            for (int j = 0; j < allUserList.size(); j++) {
                if(allUserList.get(j).getId() == o.getUserId()) {
                    commodityOrderVO.setUsername(allUserList.get(j).getUsername());
                }
            }
            for (int k = 0; k < allCommodityStandardList.size(); k++) {
                if(allCommodityStandardList.get(k).getId() == o.getCommodityStandardId()) {
                    commodityOrderVO.setCommodityStandardStr(allCommodityStandardList.get(k).getCommodityAttr());
                }
            }
            commodityOrderVO.setCommodityCount(o.getCommodityCount());
            commodityOrderVO.setOrderTotalPrice(o.getOrderTotalPrice());
            commodityOrderVO.setOrderState(o.getOrderState());
            commodityOrderVO.setLinkUser(o.getLinkUser());
            commodityOrderVO.setLinkPhone(o.getLinkPhone());
            commodityOrderVO.setLinkAddress(o.getLinkAddress());
            commodityOrderVO.setLogisticsInfo(o.getLogisticsInfo());
            commodityOrderVO.setOrderRemark(o.getOrderRemark());
            commodityOrderVO.setCreateTime(o.getCreateTime());
            commodityOrderVO.setUpdateTime(o.getUpdateTime());
            //加入arrayList中
            commodityOrderVOList.add(commodityOrderVO);
        }

        return new PageBean(commodityOrderVOList.size(), commodityOrderVOList);
    }

    /**
     * 订单排序接口(根据下单时间升降序、订单更新时间升降序、订单总价升降序 进行排序)
     */
    @Override
    public PageBean getCommodityOrderListSort(String fieldName, String sortRule) {
        ArrayList<CommodityOrder> commodityOrderList = null;
        try {
            commodityOrderList = commodityOrderMapper.getCommodityOrderListSort(fieldName, sortRule);
        } catch (Exception e) {
            e.printStackTrace();
        }

        //对查询到的集合数据中的相关字段进行替换commodity_id -> 商品名称  user_id -> 用户名  commodity_standard_id -> 规格名称
        List<Commodity> commodityList = commodityMapper.getAllCommodityList();  //查询所有商品数据
        List<User> allUserList = userMapper.getAllUserList();  //查询所有平台会员用户
        List<CommodityStandard> allCommodityStandardList = commodityStandardMapper.getAllCommodityStandardList();  //查询所有的规格信息
        List<CommodityOrderVO> commodityOrderVOList = new ArrayList<>();
        for(CommodityOrder o : commodityOrderList) {
            CommodityOrderVO commodityOrderVO = new CommodityOrderVO();
            commodityOrderVO.setId(o.getId());
            commodityOrderVO.setOrderId(o.getOrderId());
            for (int i = 0; i < commodityList.size(); i++) {
                if(commodityList.get(i).getCommodityId() == o.getCommodityId()) {
                    commodityOrderVO.setCommodityName(commodityList.get(i).getCommodityName());
                }
            }
            for (int j = 0; j < allUserList.size(); j++) {
                if(allUserList.get(j).getId() == o.getUserId()) {
                    commodityOrderVO.setUsername(allUserList.get(j).getUsername());
                }
            }
            for (int k = 0; k < allCommodityStandardList.size(); k++) {
                if(allCommodityStandardList.get(k).getId() == o.getCommodityStandardId()) {
                    commodityOrderVO.setCommodityStandardStr(allCommodityStandardList.get(k).getCommodityAttr());
                }
            }
            commodityOrderVO.setCommodityCount(o.getCommodityCount());
            commodityOrderVO.setOrderTotalPrice(o.getOrderTotalPrice());
            commodityOrderVO.setOrderState(o.getOrderState());
            commodityOrderVO.setLinkUser(o.getLinkUser());
            commodityOrderVO.setLinkPhone(o.getLinkPhone());
            commodityOrderVO.setLinkAddress(o.getLinkAddress());
            commodityOrderVO.setLogisticsInfo(o.getLogisticsInfo());
            commodityOrderVO.setOrderRemark(o.getOrderRemark());
            commodityOrderVO.setCreateTime(o.getCreateTime());
            commodityOrderVO.setUpdateTime(o.getUpdateTime());
            //加入arrayList中
            commodityOrderVOList.add(commodityOrderVO);
        }

        return new PageBean(commodityOrderVOList.size(), commodityOrderVOList);
    }

    /**
     * 订单模糊搜索(根据订单号 或者 下单用户 进行模糊匹配)
     */
    @Override
    public PageBean getCommodityOrderListByUserNameOrOrderId(Map<String, String> map) {
        //准备List<CommodityOrder> commodityOrderList对象
        List<CommodityOrder> commodityOrderList = null;
        //根据传递的参数情况调用不同的CommodityOrderMapper方法
        String orderId = map.get("orderId");  //该参数对应--->根据订单号查询订单
        String username = map.get("userName");  //该参数对应--->根据下单用户名查询订单
        if(orderId != "" && orderId != null){
            //根据订单号(订单号字符串)查询订单信息
            commodityOrderList = commodityOrderMapper.getCommodityOrderListByOrderId(orderId);
        } else if (username != "" && username != null){
            //根据下单用户名查询订单信息
            List<Integer> userIds = new ArrayList<>();
            //1.查询所有的平台会员用户表，根据前端传递过来的username进行模糊匹配，筛选出用户id数组
            List<User> userList = userMapper.getUserListByLikeUsername(username);
            Iterator<User> iterator = userList.iterator();
            while (iterator.hasNext()){
                userIds.add(iterator.next().getId());
            }
            //2.查询
            commodityOrderList = commodityOrderMapper.getCommodityOrderListByUserIds(userIds);
        } else {
            //参数匹配不到
            return new PageBean(0,null);
        }

        //对查询到的集合数据中的相关字段进行替换commodity_id -> 商品名称  user_id -> 用户名  commodity_standard_id -> 规格名称
        List<Commodity> commodityList = commodityMapper.getAllCommodityList();  //查询所有商品数据
        List<User> allUserList = userMapper.getAllUserList();  //查询所有平台会员用户
        List<CommodityStandard> allCommodityStandardList = commodityStandardMapper.getAllCommodityStandardList();  //查询所有的规格信息
        List<CommodityOrderVO> commodityOrderVOList = new ArrayList<>();
        for(CommodityOrder o : commodityOrderList) {
            CommodityOrderVO commodityOrderVO = new CommodityOrderVO();
            commodityOrderVO.setId(o.getId());
            commodityOrderVO.setOrderId(o.getOrderId());
            for (int i = 0; i < commodityList.size(); i++) {
                if(commodityList.get(i).getCommodityId() == o.getCommodityId()) {
                    commodityOrderVO.setCommodityName(commodityList.get(i).getCommodityName());
                }
            }
            for (int j = 0; j < allUserList.size(); j++) {
                if(allUserList.get(j).getId() == o.getUserId()) {
                    commodityOrderVO.setUsername(allUserList.get(j).getUsername());
                }
            }
            for (int k = 0; k < allCommodityStandardList.size(); k++) {
                if(allCommodityStandardList.get(k).getId() == o.getCommodityStandardId()) {
                    commodityOrderVO.setCommodityStandardStr(allCommodityStandardList.get(k).getCommodityAttr());
                }
            }
            commodityOrderVO.setCommodityCount(o.getCommodityCount());
            commodityOrderVO.setOrderTotalPrice(o.getOrderTotalPrice());
            commodityOrderVO.setOrderState(o.getOrderState());
            commodityOrderVO.setLinkUser(o.getLinkUser());
            commodityOrderVO.setLinkPhone(o.getLinkPhone());
            commodityOrderVO.setLinkAddress(o.getLinkAddress());
            commodityOrderVO.setLogisticsInfo(o.getLogisticsInfo());
            commodityOrderVO.setOrderRemark(o.getOrderRemark());
            commodityOrderVO.setCreateTime(o.getCreateTime());
            commodityOrderVO.setUpdateTime(o.getUpdateTime());
            //加入arrayList中
            commodityOrderVOList.add(commodityOrderVO);
        }
        return new PageBean(commodityOrderVOList.size(),commodityOrderVOList);
    }

    /*****************************************************************************************/

    /**
     * 创建平台订单
     * @@仅供测试使用
     */
    @Override
    public boolean saveCommodityOrder(CommodityOrder commodityOrder) {
        try {
            commodityOrderMapper.saveCommodityOrder(commodityOrder);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
